/**
 * May 21, 2014 3:44:38 PM
 */
package elitech.vietnam.copyweatherfashion.dialog;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;
import android.widget.PopupWindow;
import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.R;
import elitech.vietnam.copyweatherfashion.adapters.NotifyArrayAdapter;
import elitech.vietnam.copyweatherfashion.entities.Notify;
import elitech.vietnam.copyweatherfashion.wsclient.Service;

/**
 * @author Cong
 *
 */
public class NotifyWindow extends PopupWindow {
	
	MainActivity mActivity;
	public ListView mListView;
	View mLoading, mNoData;
	public NotifyArrayAdapter mAdapter;

	@Override
	public void setContentView(View contentView) {
		super.setContentView(contentView);
		mListView = (ListView) contentView.findViewById(R.id.notiListview);
		mLoading = contentView.findViewById(R.id.prgLoading);
		mNoData = contentView.findViewById(R.id.txtNodata);
		mAdapter = new NotifyArrayAdapter(mActivity, R.layout.notification_list_item, mActivity.mListNotify);
		mListView.setAdapter(mAdapter);
	}
	
	@Override
	public void showAsDropDown(View anchor) {
		super.showAsDropDown(anchor);
		if (mActivity.mListNotify.isEmpty())
			new GetData().execute();
		else
			mAdapter.notifyDataSetChanged();
	}
	
	public NotifyWindow() {
		super();
	}

	public NotifyWindow(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public NotifyWindow(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public NotifyWindow(Context context) {
		super(context);
		mActivity = (MainActivity) context;
	}

	public NotifyWindow(int width, int height) {
		super(width, height);
	}

	public NotifyWindow(View contentView, int width, int height, boolean focusable) {
		super(contentView, width, height, focusable);
	}

	public NotifyWindow(View contentView, int width, int height) {
		super(contentView, width, height);
	}

	public NotifyWindow(View contentView) {
		super(contentView);
	}
	
	class GetData extends AsyncTask<Integer, Integer, Boolean> {
		List<Notify> rs = new ArrayList<>();
		@Override
		protected void onPreExecute() {
			if (mListView.getVisibility() != View.GONE) {
				mListView.setVisibility(View.GONE);
				mLoading.setVisibility(View.VISIBLE);
				mNoData.setVisibility(View.GONE);
			}
		}
		@Override
		protected Boolean doInBackground(Integer... params) {
			try {
				rs = new Service().getNotifications(mActivity.mUser.Id, 0, 10);
			} catch (IOException | XmlPullParserException e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}
		@Override
		protected void onPostExecute(Boolean result) {
			if (!isShowing())
				return;
			if (!result) {
			} else if (rs == null || rs.isEmpty()){
				mListView.setVisibility(View.GONE);
				mLoading.setVisibility(View.GONE);
				mNoData.setVisibility(View.VISIBLE);
			} else {
				mActivity.mListNotify.clear();
				mActivity.mListNotify.addAll(rs);
				mListView.setVisibility(View.VISIBLE);
				mLoading.setVisibility(View.GONE);
				mNoData.setVisibility(View.GONE);
			}
		}
	}
	
	class GetMoreData extends AsyncTask<Integer, Integer, Boolean> {
		List<Notify> rs = new ArrayList<>();
		@Override
		protected void onPreExecute() {
			if (mListView.getVisibility() != View.GONE) {
				mListView.setVisibility(View.GONE);
				mLoading.setVisibility(View.VISIBLE);
				mNoData.setVisibility(View.GONE);
			}
		}
		@Override
		protected Boolean doInBackground(Integer... params) {
			try {
				rs = new Service().getNotifications(mActivity.mUser.Id, mActivity.mListNotify.size(), 10);
			} catch (IOException | XmlPullParserException e) {
				e.printStackTrace();
				return false;
			}
			return true;
		}
		@Override
		protected void onPostExecute(Boolean result) {
			if (!isShowing())
				return;
			if (!result) {
			} else if (rs == null || rs.isEmpty()){
				mListView.setVisibility(View.GONE);
				mLoading.setVisibility(View.GONE);
				mNoData.setVisibility(View.VISIBLE);
			} else {
				mActivity.mListNotify.clear();
				mActivity.mListNotify.addAll(rs);
				mListView.setVisibility(View.VISIBLE);
				mLoading.setVisibility(View.GONE);
				mNoData.setVisibility(View.GONE);
				mAdapter.notifyDataSetChanged();
			}
		}
	}
}
